<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Title           Photo model
 *
 * @package        appworkz
 * Location        application/frontend/models/photo
 *
 * @author         Ruwan - ruwanpathmalal@gmail.com
 * @copyright      appworkz
 *
 * created on      28-012-2012, 10:30AM by Ruwan
 * updated on      dd-mm-yyy, hh:mm by {name}
 *
 * Description     Functionality relavant to photos
 *
 * */

//  CI 2.0 Compatibility
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }


class Photo_model extends CI_Model
{

    /**
     * Holds an array of tables used
     *
     * @var string
     **/
    public $mastables = array();

    /**
     * __construct
     *
     * @param none
     * @access public
     * @return void
     * @author Ruwan - ruwanpathmalal@gmail.com
     **/
    public function __construct()
    {
        parent::__construct();
        $this->mastables['album_image'] = 'album_image';
    }

    /**
     * This will add a new photo
     *
     * @param array $data
     * @access public
     * @return int $id
     * @author Ruwan - ruwanpathmalal@gmail.com
     **/
    public function add($data = array())
    {
        $this->db->insert($this->mastables['album_image'], $data);
		return $this->db->insert_id();
    }

    /**
     * Update business table to put default image
     *
     * @param int $business_id
     * @param array $data
     * @access public
     * @return boolean
     * @author Ruwan - ruwanpathmalal@gmail.com
     **/
    public function update_business($business_id,$data){
    	$this->db->where('BusinessID', $business_id);
    	$this->db->update('business', $data);
    }
    
    /**
     * get album photos
     *
     * @param int $album_id
     * @param int $limit
     * @param int $offset
     * @access public
     * @return array
     * @author Ruwan - ruwanpathmalal@gmail.com
     **/
    public function get_album_photos($album_id,$limit=NULL,$offset=NULL) {
    	$this->db->select('AlbumImageID,AlbumID,ImageUrl,ImageText,LinkTypeID');
    	$this->db->from($this->mastables['album_image']);
    	$this->db->where(array('AlbumID'=>$album_id,'IsDelete'=>0));
    	if($limit != NULL){
    		if($offset != NULL){
    			$this->db->limit($limit,$offset);
    		} else {
    			$this->db->limit($limit);
    		}
    	}
    	$query = $this->db->get();
    	return $query->result_array();
    }
    
    /**
     * mark as delete photo
     *
     * @param int $business_id
     * @param int $album_id
     * @access public
     * @return boolean
     * @author Ruwan - ruwanpathmalal@gmail.com
     **/
    public function delete_album_photo($business_id,$album_id,$photo_id=NULL) {
    	$where = array('ai.AlbumID'=>$album_id,'ai.IsDelete'=>0);
    	if($photo_id){
    		$where['AlbumImageID'] = $photo_id;
    	}
    	return $this->db->update($this->mastables['album_image']. ' AS ai
    			LEFT JOIN business_album as ba ON (ai.AlbumID=ba.AlbumID AND ba.BusinessID='.$business_id.')',array('ai.IsDelete'=>1),$where);
    }    
}

/* End of file photo_model.php */
/* Location: ./application/frontend/models/activity/photo/photo_model.php */